{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   instant      dteday  season  yr  mnth  holiday  weekday  workingday  \\\n0        1  2011-01-01       1   0     1        0        6           0   \n1        2  2011-01-02       1   0     1        0        0           0   \n2        3  2011-01-03       1   0     1        0        1           1   \n3        4  2011-01-04       1   0     1        0        2           1   \n4        5  2011-01-05       1   0     1        0        3           1   \n\n   weathersit      temp     atemp       hum  windspeed  casual  registered  \\\n0           2  0.344167  0.363625  0.805833   0.160446     331         654   \n1           2  0.363478  0.353739  0.696087   0.248539     131         670   \n2           1  0.196364  0.189405  0.437273   0.248309     120        1229   \n3           1  0.200000  0.212122  0.590435   0.160296     108        1454   \n4           1  0.226957  0.229270  0.436957   0.186900      82        1518   \n\n    cnt  \n0   985  \n1   801  \n2  1349  \n3  1562  \n4  1600  \n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import seaborn as sn\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "\n",
    "params = {\n",
    "    \"legend.fontsize\": \"x-large\",\n",
    "    \"figure.figsize\": (30, 10),\n",
    "    \"axes.labelsize\": \"x-large\",\n",
    "    \"axes.titlesize\": \"x-large\",\n",
    "    \"xtick.labelsize\": \"x-large\",\n",
    "    \"ytick.labelsize\": \"x-large\"\n",
    "}\n",
    "\n",
    "sn.set_style(\"whitegrid\")\n",
    "sn.set_context(\"talk\")\n",
    "plt.rcParams.update(params)\n",
    "pd.set_option(\"max_colwidth\", 600)\n",
    "\n",
    "train = pd.read_csv(\"data/day.csv\")\n",
    "print(train.head())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\nRangeIndex: 731 entries, 0 to 730\nData columns (total 16 columns):\ninstant       731 non-null int64\ndteday        731 non-null object\nseason        731 non-null int64\nyr            731 non-null int64\nmnth          731 non-null int64\nholiday       731 non-null int64\nweekday       731 non-null int64\nworkingday    731 non-null int64\nweathersit    731 non-null int64\ntemp          731 non-null float64\natemp         731 non-null float64\nhum           731 non-null float64\nwindspeed     731 non-null float64\ncasual        731 non-null int64\nregistered    731 non-null int64\ncnt           731 non-null int64\ndtypes: float64(4), int64(11), object(1)\nmemory usage: 91.5+ KB\nNone\n"
     ]
    }
   ],
   "source": [
    "print(train.info())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          instant      season          yr        mnth     holiday     weekday  \\\ncount  731.000000  731.000000  731.000000  731.000000  731.000000  731.000000   \nmean   366.000000    2.496580    0.500684    6.519836    0.028728    2.997264   \nstd    211.165812    1.110807    0.500342    3.451913    0.167155    2.004787   \nmin      1.000000    1.000000    0.000000    1.000000    0.000000    0.000000   \n25%    183.500000    2.000000    0.000000    4.000000    0.000000    1.000000   \n50%    366.000000    3.000000    1.000000    7.000000    0.000000    3.000000   \n75%    548.500000    3.000000    1.000000   10.000000    0.000000    5.000000   \nmax    731.000000    4.000000    1.000000   12.000000    1.000000    6.000000   \n\n       workingday  weathersit        temp       atemp         hum   windspeed  \\\ncount  731.000000  731.000000  731.000000  731.000000  731.000000  731.000000   \nmean     0.683995    1.395349    0.495385    0.474354    0.627894    0.190486   \nstd      0.465233    0.544894    0.183051    0.162961    0.142429    0.077498   \nmin      0.000000    1.000000    0.059130    0.079070    0.000000    0.022392   \n25%      0.000000    1.000000    0.337083    0.337842    0.520000    0.134950   \n50%      1.000000    1.000000    0.498333    0.486733    0.626667    0.180975   \n75%      1.000000    2.000000    0.655417    0.608602    0.730209    0.233214   \nmax      1.000000    3.000000    0.861667    0.840896    0.972500    0.507463   \n\n            casual   registered          cnt  \ncount   731.000000   731.000000   731.000000  \nmean    848.176471  3656.172367  4504.348837  \nstd     686.622488  1560.256377  1937.211452  \nmin       2.000000    20.000000    22.000000  \n25%     315.500000  2497.000000  3152.000000  \n50%     713.000000  3662.000000  4548.000000  \n75%    1096.000000  4776.500000  5956.000000  \nmax    3410.000000  6946.000000  8714.000000  \n"
     ]
    }
   ],
   "source": [
    "print(train.describe())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\nseason属性的不同取值和出现的次数为:\n3    188\n2    184\n1    181\n4    178\nName: season, dtype: int64\n\nmnth属性的不同取值和出现的次数为:\n12    62\n10    62\n8     62\n7     62\n5     62\n3     62\n1     62\n11    60\n9     60\n6     60\n4     60\n2     57\nName: mnth, dtype: int64\n\nweathersit属性的不同取值和出现的次数为:\n1    463\n2    247\n3     21\nName: weathersit, dtype: int64\n\nweekday属性的不同取值和出现的次数为:\n6    105\n1    105\n0    105\n5    104\n4    104\n3    104\n2    104\nName: weekday, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "categorical_features = [\"season\", \"mnth\", \"weathersit\", \"weekday\"]\n",
    "for col in categorical_features:\n",
    "    print(\"\\n{}属性的不同取值和出现的次数为:\".format(col))\n",
    "    print(train[col].value_counts())\n",
    "    train[col] = train[col].astype(\"object\")\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 数值特征的分布\n",
    "numerical_features = [\"temp\", \"atemp\", \"hum\", \"windspeed\"]\n",
    "train[numerical_features].hist()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 特征和目标之间的关系\n",
    "# 年份和骑行量的分布\n",
    "sn.violinplot(data=train[[\"yr\", 'cnt']], x=\"yr\", y=\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一年中每天的骑行量\n",
    "train[\"date\"] = pd.to_datetime(train[\"dteday\"])\n",
    "train[\"dayofyear\"] = train[\"date\"].dt.dayofyear\n",
    "fig, ax = plt.subplots()\n",
    "sn.pointplot(data=train[[\"dayofyear\", \"cnt\", \"yr\"]], \n",
    "             x=\"dayofyear\", y=\"cnt\", hue=\"yr\", ax=ax)\n",
    "ax.set(title=\"daily distribution of counts\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 季节与骑行量的关系\n",
    "fig, ax = plt.subplots()\n",
    "sn.violinplot(data=train[[\"season\", \"cnt\", \"yr\"]], \n",
    "              x=\"season\", y=\"cnt\", hue=\"yr\", ax=ax)\n",
    "ax.set(title=\"season distribution of counts\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 月份与骑行量的关系\n",
    "fig, ax = plt.subplots()\n",
    "sn.barplot(data=train[[\"mnth\", \"cnt\"]],\n",
    "              x=\"mnth\", y=\"cnt\", ax=ax)\n",
    "ax.set(title=\"mnth distribution of counts\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 天气与骑行量的关系\n",
    "fig, ax = plt.subplots()\n",
    "sn.barplot(data=train[[\"weathersit\", \"cnt\"]],\n",
    "           x=\"weathersit\", y=\"cnt\", ax=ax)\n",
    "ax.set(title=\"weathersit distribution of counts\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 工作日和节假日的骑行量分布\n",
    "plt.figure(figsize=(30, 30))\n",
    "fig, (ax1, ax2) = plt.subplots(ncols=2)\n",
    "sn.barplot(data=train[[\"holiday\", \"cnt\"]],\n",
    "           x=\"holiday\", y=\"cnt\", ax=ax1)\n",
    "ax1.set(title=\"holiday distribution of counts\")\n",
    "sn.barplot(data=train[[\"workingday\", \"cnt\"]],\n",
    "           x=\"workingday\", y=\"cnt\", ax=ax2)\n",
    "ax2.set(title=\"workingday distribution of counts\")\n",
    "plt.show()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数值型特征和cnt之间的关系\n",
    "plt.figure(figsize=(30, 30))\n",
    "fig, ax = plt.subplots()\n",
    "corrMat = train[[\"temp\", \"atemp\", \"hum\", \"windspeed\", \"casual\", \"registered\", \"cnt\"]].corr()\n",
    "mask = np.array(corrMat)\n",
    "mask[np.tril_indices_from(mask)] = False\n",
    "sn.heatmap(corrMat, mask=mask, vmax=0.8, square=True, annot=True, ax=ax)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  mnth_4  \\\n0         1         0         0         0       1       0       0       0   \n1         1         0         0         0       1       0       0       0   \n2         1         0         0         0       1       0       0       0   \n3         1         0         0         0       1       0       0       0   \n4         1         0         0         0       1       0       0       0   \n\n   mnth_5  mnth_6  ...  weathersit_1  weathersit_2  weathersit_3  weekday_0  \\\n0       0       0  ...             0             1             0          0   \n1       0       0  ...             0             1             0          1   \n2       0       0  ...             1             0             0          0   \n3       0       0  ...             1             0             0          0   \n4       0       0  ...             1             0             0          0   \n\n   weekday_1  weekday_2  weekday_3  weekday_4  weekday_5  weekday_6  \n0          0          0          0          0          0          1  \n1          0          0          0          0          0          0  \n2          1          0          0          0          0          0  \n3          0          1          0          0          0          0  \n4          0          0          1          0          0          0  \n\n[5 rows x 26 columns]\n"
     ]
    }
   ],
   "source": [
    "# 特征工程\n",
    "# 类别型特征编码\n",
    "categorical_features = [\"season\", \"mnth\", \"weathersit\", \"weekday\"]\n",
    "for col in categorical_features:\n",
    "    train[col] = train[col].astype(\"object\")\n",
    "    \n",
    "x_train_cat = train[categorical_features]\n",
    "x_train_cat = pd.get_dummies(x_train_cat)\n",
    "print(x_train_cat.head())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       temp     atemp       hum  windspeed\n0  0.355170  0.373517  0.828620   0.284606\n1  0.379232  0.360541  0.715771   0.466215\n2  0.171000  0.144830  0.449638   0.465740\n3  0.175530  0.174649  0.607131   0.284297\n4  0.209120  0.197158  0.449313   0.339143\n   season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  mnth_4  \\\n0         1         0         0         0       1       0       0       0   \n1         1         0         0         0       1       0       0       0   \n2         1         0         0         0       1       0       0       0   \n3         1         0         0         0       1       0       0       0   \n4         1         0         0         0       1       0       0       0   \n\n   mnth_5  mnth_6  ...  weekday_3  weekday_4  weekday_5  weekday_6      temp  \\\n0       0       0  ...          0          0          0          1  0.355170   \n1       0       0  ...          0          0          0          0  0.379232   \n2       0       0  ...          0          0          0          0  0.171000   \n3       0       0  ...          0          0          0          0  0.175530   \n4       0       0  ...          1          0          0          0  0.209120   \n\n      atemp       hum  windspeed  holiday  workingday  \n0  0.373517  0.828620   0.284606        0           0  \n1  0.360541  0.715771   0.466215        0           0  \n2  0.144830  0.449638   0.465740        0           1  \n3  0.174649  0.607131   0.284297        0           1  \n4  0.197158  0.449313   0.339143        0           1  \n\n[5 rows x 32 columns]\n<class 'pandas.core.frame.DataFrame'>\nRangeIndex: 731 entries, 0 to 730\nData columns (total 32 columns):\nseason_1        731 non-null uint8\nseason_2        731 non-null uint8\nseason_3        731 non-null uint8\nseason_4        731 non-null uint8\nmnth_1          731 non-null uint8\nmnth_2          731 non-null uint8\nmnth_3          731 non-null uint8\nmnth_4          731 non-null uint8\nmnth_5          731 non-null uint8\nmnth_6          731 non-null uint8\nmnth_7          731 non-null uint8\nmnth_8          731 non-null uint8\nmnth_9          731 non-null uint8\nmnth_10         731 non-null uint8\nmnth_11         731 non-null uint8\nmnth_12         731 non-null uint8\nweathersit_1    731 non-null uint8\nweathersit_2    731 non-null uint8\nweathersit_3    731 non-null uint8\nweekday_0       731 non-null uint8\nweekday_1       731 non-null uint8\nweekday_2       731 non-null uint8\nweekday_3       731 non-null uint8\nweekday_4       731 non-null uint8\nweekday_5       731 non-null uint8\nweekday_6       731 non-null uint8\ntemp            731 non-null float64\natemp           731 non-null float64\nhum             731 non-null float64\nwindspeed       731 non-null float64\nholiday         731 non-null int64\nworkingday      731 non-null int64\ndtypes: float64(4), int64(2), uint8(26)\nmemory usage: 53.0 KB\nNone\n"
     ]
    }
   ],
   "source": [
    "# 数值型特征标准化\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "\n",
    "mn_x = MinMaxScaler()\n",
    "numerical_features = [\"temp\", \"atemp\", \"hum\", \"windspeed\"]\n",
    "temp = mn_x.fit_transform(train[numerical_features])\n",
    "x_train_num = pd.DataFrame(data=temp, columns=numerical_features, index=train.index)\n",
    "print(x_train_num.head())\n",
    "\n",
    "x_train = pd.concat([x_train_cat, x_train_num, train[\"holiday\"], train[\"workingday\"]],\n",
    "                    axis=1, ignore_index=False)\n",
    "print(x_train.head())\n",
    "print(x_train.info())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   instant  season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  \\\n0        1         1         0         0         0       1       0       0   \n1        2         1         0         0         0       1       0       0   \n2        3         1         0         0         0       1       0       0   \n3        4         1         0         0         0       1       0       0   \n4        5         1         0         0         0       1       0       0   \n\n   mnth_4  mnth_5  ...  weekday_5  weekday_6      temp     atemp       hum  \\\n0       0       0  ...          0          1  0.355170  0.373517  0.828620   \n1       0       0  ...          0          0  0.379232  0.360541  0.715771   \n2       0       0  ...          0          0  0.171000  0.144830  0.449638   \n3       0       0  ...          0          0  0.175530  0.174649  0.607131   \n4       0       0  ...          0          0  0.209120  0.197158  0.449313   \n\n   windspeed  holiday  workingday  yr   cnt  \n0   0.284606        0           0   0   985  \n1   0.466215        0           0   0   801  \n2   0.465740        0           1   0  1349  \n3   0.284297        0           1   0  1562  \n4   0.339143        0           1   0  1600  \n\n[5 rows x 35 columns]\n<class 'pandas.core.frame.DataFrame'>\nRangeIndex: 731 entries, 0 to 730\nData columns (total 35 columns):\ninstant         731 non-null int64\nseason_1        731 non-null uint8\nseason_2        731 non-null uint8\nseason_3        731 non-null uint8\nseason_4        731 non-null uint8\nmnth_1          731 non-null uint8\nmnth_2          731 non-null uint8\nmnth_3          731 non-null uint8\nmnth_4          731 non-null uint8\nmnth_5          731 non-null uint8\nmnth_6          731 non-null uint8\nmnth_7          731 non-null uint8\nmnth_8          731 non-null uint8\nmnth_9          731 non-null uint8\nmnth_10         731 non-null uint8\nmnth_11         731 non-null uint8\nmnth_12         731 non-null uint8\nweathersit_1    731 non-null uint8\nweathersit_2    731 non-null uint8\nweathersit_3    731 non-null uint8\nweekday_0       731 non-null uint8\nweekday_1       731 non-null uint8\nweekday_2       731 non-null uint8\nweekday_3       731 non-null uint8\nweekday_4       731 non-null uint8\nweekday_5       731 non-null uint8\nweekday_6       731 non-null uint8\ntemp            731 non-null float64\natemp           731 non-null float64\nhum             731 non-null float64\nwindspeed       731 non-null float64\nholiday         731 non-null int64\nworkingday      731 non-null int64\nyr              731 non-null int64\ncnt             731 non-null int64\ndtypes: float64(4), int64(5), uint8(26)\nmemory usage: 70.1 KB\nNone\n"
     ]
    }
   ],
   "source": [
    "FE_train = pd.concat([train[\"instant\"], x_train, train[\"yr\"], train[\"cnt\"]], axis=1)\n",
    "FE_train.to_csv(\"out/FE_day.csv\", index=False)\n",
    "print(FE_train.head())\n",
    "print(FE_train.info())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
